package com.app.zenmewan.controller;

import com.app.zenmewan.entity.Statstic;
import com.app.zenmewan.service.StatsticService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (Statstic)表控制层
 *
 * @author makejava
 * @since 2020-05-12 16:44:45
 */
@RestController
@RequestMapping("statstic")
public class StatsticController {
    /**
     * 服务对象
     */
    @Resource
    private StatsticService statsticService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Statstic selectOne(Integer id) {
        return this.statsticService.queryById(id);
    }
    @GetMapping("list")
    public ModelAndView list(){
        ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());
        List<Statstic> l =  this.statsticService.queryAll(new Statstic());
        ArrayList<String> datalist = new ArrayList<>();
        ArrayList<Integer> countlist = new ArrayList<>();
        ArrayList<Integer> artlist = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (Statstic statstic : l) {
            String s = statstic.getDate();
            datalist.add(s);
            countlist.add(statstic.getVisitCount());
            artlist.add(statstic.getArticleCount());
        }
        modelAndView.addObject("dlist",datalist);
        modelAndView.addObject("clist",countlist);
        modelAndView.addObject("alist",artlist);
        return modelAndView;
    }
    @GetMapping("test")
    public void test(){
        List<Statstic> list= this.statsticService.queryAll(new Statstic());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println("today"+sdf.format(new Date()));
        for (Statstic statstic : list) {
            System.out.println(sdf.format(statstic.getDate()));
        }
    }
    @GetMapping("addcount")
    public void addcount(String type){
        System.out.println("addcount is driveding.");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String today = sdf.format(new Date());
        Statstic statstic = new Statstic();
        statstic.setDate(today);
        List<Statstic> list= this.statsticService.queryAll(statstic);
        if(list.size()==0){
            statstic.setVisitCount(0);
            statstic.setArticleCount(0);
            if("art".equals(type)){
                statstic.setArticleCount(1);
            }else if("visit".equals(type)){
                statstic.setVisitCount(1);
            }
            this.statsticService.insert(statstic);
        }else{
            statstic = list.get(0);
            if("art".equals(type)){
                statstic.setArticleCount(statstic.getArticleCount()+1);
            }else if("visit".equals(type)){
                statstic.setVisitCount(1+statstic.getVisitCount());
            }
            this.statsticService.update(statstic);
        }
    }
}